import LeafManager from './LeafManager'
import NodeManager from './NodeManager'
import { insertDom } from './util'

class BranchManager {
    constructor(data, container, option, imageList) {
        this.option = option
        this.imageList = imageList
        this.init(data, container)
    }

    init(data, container) {
        // 雪花图容器
        const wrapper = insertDom(container, { className: 'sc-container' })
        // dom分组
        const domGroup = this.initGroup(wrapper)
        this.initCenter(data, wrapper)
        // 处理数据
        this.loopData(data, domGroup)
    }

    initGroup(wrapper) {
        // 创建圆周节点集合
        const nodeGroup = insertDom(wrapper, { className: 'sc-child-group' })
        // 创建连线节点集合
        const lineGroup = insertDom(wrapper, { className: 'sc-line-group' })
        return { nodeGroup, lineGroup }
    }

    initCenter(data, wrapper) {
        const center = insertDom(wrapper, { className: 'sc-center-group' })
        new NodeManager(data, center, this.option, 0, true)
    }

    loopData(data, domGroup) {
        const { nodeGroup, lineGroup } = domGroup
        data.children.forEach((dataItem, index) => {
            const deg = 360 / data.children.length * index
            if (dataItem.children?.length) {
                // 多层嵌套
            } else {
                new LeafManager(dataItem, { nodeGroup, lineGroup }, this.option, deg, this.imageList, index)
            }
        })
    }
}

export default BranchManager